<template>
  <el-form class="find-pass-step1" ref="form" :model="formData" :rules="rules">
    <el-form-item prop="password">
      <el-input
        v-model="formData.password"
        :placeholder="$tInput('field.password')"
        type="password"
        prefix-icon="el-icon-lock"
      >
      </el-input>
    </el-form-item>
    <el-form-item prop="password2">
      <el-input
        v-model="formData.password2"
        :placeholder="$tInput('field.password2')"
        type="password"
        prefix-icon="el-icon-lock"
      >
      </el-input>
    </el-form-item>
    <el-button
      class="w-100"
      type="primary"
      round
      @click="submitForm"
      :loading="submiting"
      >保存修改</el-button
    >
  </el-form>
</template>

<script>
import { findPwdUpdateSchool } from 'services/school/auth'
import { findPwdUpdateCompany } from 'services/company/auth'
import { findPwdUpdateStudent } from 'services/student/auth'
import { mapState } from 'vuex'
export default {
  name: 'FindPassStep2',
  props: {
    verifyData: {
      type: Object
    }
  },
  data() {
    return {
      formData: {},
      submiting: false
    }
  },
  computed: {
    ...mapState(['clientType']),
    rules() {
      return {
        password: this.$rule('field.password', {
          required: true
        }),
        password2: this.$rule('field.password2', {
          required: true
        })
      }
    }
  },
  methods: {
    async submitForm() {
      try {
        const valid = await this.$refs.form.validate()
        if (!valid) {
          return
        }
        this.submiting = true
        let service
        if (this.clientType === 'school') {
          service = findPwdUpdateSchool
        } else if (this.clientType === 'company') {
          service = findPwdUpdateCompany
        } else {
          service = findPwdUpdateStudent
        }
        await service({
          ...this.verifyData,
          ...this.formData
        })
        this.$emit('success')
      } catch ({ msg }) {
        msg && this.$error(msg)
      } finally {
        this.submiting = false
      }
    }
  }
}
</script>
